package com.lemon.smartsafetynew.ui.adapter.user;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.ApplicationInfo;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.entity.User;
import com.lemon.smartsafetynew.util.common.CommonUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Andrew on 2017/9/27.
 */

public class UserAccountChooseAdapter extends LemonRcvAdapter<User> {

    private RecyclerTClickListener<User> mListener;

    public UserAccountChooseAdapter(@Nullable List<User> data, RecyclerTClickListener<User> mListener) {
        super(data);
        this.mListener = mListener;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new ItemAdapter();
    }

    public class ItemAdapter implements AdapterItem<User> {


        private User mUser;
        private int mPosition;

        @Bind(R.id.user_type_tv)
        TextView userTypeTv;
        @Bind(R.id.company_name_tv)
        TextView companyNameTv;
        @Bind(R.id.layout)
        LinearLayout layout;

        @Override
        public int getLayoutResId() {
            return R.layout.item_user_account;
        }

        @Override
        public void bindViews(View root) {
            ButterKnife.bind(this, root);
        }

        @Override
        public void setViews() {
            layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(mUser, mPosition);
                }
            });
        }

        @Override
        public void handleData(User user, int position) {

            mPosition = position;
            mUser = user;

            companyNameTv.setText(CommonUtil.avoidNullString(mUser.getCompanyName()));

            //fix bug#372 begin
            if(mUser.getUserType() != null) {
                String type = ApplicationInfo.userTypeMap.get(mUser.getUserType());
                userTypeTv.setText(CommonUtil.avoidNullString(type));
            } else {
                userTypeTv.setText(CommonUtil.avoidNullString(mUser.getUserType()));
            }
            //fix bug#372 end
        }
    }
}
